<?php
class Manage extends CI_Controller {

	function index() {
		show_404(base_url());
	}

	function idea() {
		//Manage idea
		$this -> load -> model('idea_model');
		$ideaId = $this -> input -> post('idea_id');
		
		$data['idea'] = $this -> idea_model -> getIdeaById($ideaId);
		$data['idea_tags'] = $this -> idea_model -> getIdeaTags($ideaId);
		
		$this -> load -> view('view_manage', $data);
	}

	function updateIdea() {
		//Handle update request from user
		include("authenticate.php");
		
		$this -> load -> model('idea_model');
		$this -> load -> library('form_validation');

		//form validation
		$this -> form_validation -> set_rules('idea_overview', 'Idea Overview', 'required');
		$this -> form_validation -> set_rules('idea_problem', 'Problem', 'required');
		$this -> form_validation -> set_rules('idea_video_link', 'Video Link', 'callback_YouTubeURLCheck');
		$this -> form_validation -> set_rules('idea_solution', 'Solution', 'required');

		if ($this -> form_validation -> run() == FALSE) {

			//redirect to form
			$this -> idea($this -> input -> post('idea_id'));

		} else {
			//form validated

			$username = $_SESSION['username'];
			$idea_id = $this -> input -> post('idea_id');
			$overview = $this -> input -> post('idea_overview');
			$problem = $this -> input -> post('idea_problem');
			$solution = $this -> input -> post('idea_solution');
			$videoLink = $this -> input -> post('idea_video_link');
			$idea_category = $this ->input ->post('idea_category');
			$videoLink = $this -> parseYouTubeURL($videoLink);

			$data = array('idea_overview' => $overview, 
							'idea_problem' => $problem, 
							'idea_solution' => $solution, 
							'idea_video_link' => $videoLink, 
							'idea_category' => $idea_category);

			$success = $this -> idea_model -> updateIdea($idea_id, $data);
			if ($success == 1) {
				$tag = strtolower($this -> input -> post('idea_tag'));
				//check whether tag is not empty
				if (strlen(trim($tag)) != 0) {
					
					$tags = explode(';', $tag);
					$this -> idea_model -> removeIdeaTags($idea_id);
					$this -> idea_model -> addIdeaTags($idea_id, $tags);
				}
				
				//track activity
				$this->load->model('feed_model');
				$this->feed_model->trackIdeaActivity($idea_id, " has updated its information.");
				
				redirect('ideas/byId/' . $idea_id);
			}

		}
	}

	function delete() {
		//Function to delete an idea
		
		include("authenticate.php");
		
		
		$username = $_SESSION['username'];
		$idea_id = $this->input->post('idea_id');
		
		if(isset($idea_id)){
			
			$this->load->model('idea_model');
			
			$confirmDelete = $this->idea_model->deleteIdea($idea_id);
			
			if($confirmDelete == 1){
				//idea is deleted
				//track activity
				$this->load->model('feed_model');
				$this->feed_model->trackUserActivity($username, " has deleted an idea.");
				
			}
			
		}
	}

	function YouTubeURLCheck($url){
		//parse the url
		$matched = $this->parseYouTubeURL($url);
		
		//if matched
		if(strlen($matched) > 0){
			
			if(strlen($matched) != 11){
				
				//valid youtube link
				$this->form_validation->set_message('YouTubeURLCheck', 'Video Link is Invalid');
				
				return false;
				
			}else{
				
				return true;
			}
			
		}else{
			
			return true;
		
		}
	}

	function parseYouTubeURL($url){
		//search for matched regex
		$regMatch = preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $url, $matches);
		
			if($regMatch >0){
				
				return $matches[0];
				
			}else{
				
				return '';
			
			}		
	}

}
?>
